package com.powernode.service.impl;

import com.alibaba.fastjson.JSON;
import com.powernode.constant.QueueConstant;
import com.powernode.dto.SmsDto;
import com.powernode.service.SmsService;
import com.powernode.until.PhoneCodeUntil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;


@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void phoneCode(SmsDto smsDto) {

        //获取到4位验证码
        String fourCode = PhoneCodeUntil.phoneCodeUntil(4);
        smsDto.setCode(fourCode);
        stringRedisTemplate.opsForValue().set(smsDto.getPhonenum(), fourCode, Duration.ofSeconds(5L));
        rabbitTemplate.convertAndSend(QueueConstant.CODE_QUEUE, JSON.toJSONString(smsDto));
    }
}
